<?php
/**
 * RuiNuoCode低代码后台系统 - 团队十多年低代码经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 张家界瑞诺科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.ruinuocrm.com
 * =========================================================
 */
namespace app\admin\service\menu;
use app\admin\service\menu\Logic as RuinuoLogic;//数据导入表单服务
use app\common\service\Excel as ExcelService;//导入导出服务
use app\common\service\Lists as ListsService;//列表服务
use app\common\service\Power as PowerService;//权限服务
use app\common\model\AdminMenu as DataMode;//数据模型

class Lists
{
    public function Mode()
    {
        $Model=new DataMode();
        $DB=$Model->alias('a');
        return $DB;
    }
    public function SetWhere($current=1)
    {
        $Power=new PowerService();
        $where='1=1 and a.is_delete=0';

        $where.=$Power->IndexWhere('admin:menu');
        return $where;
    }

    public function QueryWhere($params = [])
    {
        $where=[];
        if (!empty($params['choose_ids'])){
            $where[] = ['a.id', 'in', $params['choose_ids']];
        }

        return $where;
    }

    public function Tab($params = [])
    {
        $data=[];
        return $data;
    }
    public function GetTab($params=[],$current=1)
    {
        return $this->Mode()->where($this->SetWhere($current))->where($this->QueryWhere($params))->count();
    }

    public function Sum($params = [])
    {
        $data=[];
        return $data;
    }

    public function GetSum($params=[],$field='id')
    {
        $current=!empty($params['current']) ? $params['current'] : 1;
        return $this->Mode()->where($this->SetWhere($current))->where($this->QueryWhere($params))->sum($field);
    }

    public function Total($params = [])
    {
        $current= !empty($params['current']) ? $params['current'] : 1;
        $count = $this->Mode()
            ->where($this->SetWhere($current))
            ->where($this->QueryWhere($params))
            ->count();
        return $count;
    }

    public function Lists($params = [])
    {
        $Power=new PowerService();
        $page = !empty($params['page']) ? $params['page'] - 1 : 0;
        $limit = !empty($params['limit']) ? $params['limit'] : 1000000;
        $current= !empty($params['current']) ? $params['current'] : 1;
        $orderRaw=!empty($params['raw']) ? $params['raw'] : 'sort asc,id desc';
        $field=$Power->IndexField('a.id,a.title,a.type,a.parent_id,a.href,a.power_node,a.sort,a.status','admin:menu');
        $rows= $this->Mode()
            ->field($field)
            ->where($this->SetWhere($current))
            ->where($this->QueryWhere($params))
            ->orderRaw($orderRaw)
            ->limit($page * $limit, $limit)
            ->select()
            ->toArray();
        $ListsService = new ListsService();
        $rows=$ListsService->ListData($rows);//转换数据
        return $rows;
    }

    /*Excel导入，根据表单导入及验证*/
    public function InExcel($file = '')
    {
        if ($file) {
            $ExcelService = new ExcelService();
            return $ExcelService->Import($file);
        }
        $arr = input("data");
        $RuinuoLogic = new RuinuoLogic();
        $data = [];
        foreach ($arr as $k => $v) {
            $item = $RuinuoLogic->Add($v);
            $v['msg'] = $item['msg'];
            $v['is_fail'] = 2;
            if ($item['code'] != 200) {
                $v['is_fail'] = 1;
            }
            $data[] = $v;
        }
        return ['code' => 200, 'msg' => '', 'data' => $data];
    }

    /*Excel导出下载，根据列表生成*/
    public function OutExcel($params = [])
    {
        $Power=new PowerService();
        $ListsService=new ListsService();
        $current= !empty($params['current']) ? $params['current'] : 1;
        $orderRaw=!empty($params['raw']) ? $params['raw'] : 'sort asc,id desc';
        $field=$Power->IndexField('a.id,a.title,a.type,a.parent_id,a.href,a.power_node,a.sort,a.status','admin:menu');
        $lists = $this->Mode()
            ->field($field)
            ->where($this->SetWhere($current))
            ->where($this->QueryWhere($params))
            ->orderRaw($orderRaw)
            ->select()
            ->toArray();
        $lists=$ListsService->ListData($lists);//转换数据
        $ExcelService = new ExcelService();
        $excel_field=[
            ['field'=>'title','title'=>'名称'],
            ['field'=>'type','title'=>'类型'],
            ['field'=>'parent_id','title'=>'上级'],
            ['field'=>'href','title'=>'访问路径'],
            ['field'=>'power_node','title'=>'授权标识'],
            ['field'=>'sort','title'=>'排序'],
            ['field'=>'status','title'=>'是否可用'],
        ];
        $data = [[
            'title' => '菜单管理',
            'cols' => $ExcelService->Filterate($excel_field,$field),//根据数据输出字段过滤cols
            'list' => $lists
        ]];
        $ExcelService->Out($data, '菜单管理');
    }

    /*Excel导入模板下载，根据表单生成*/
    public function TemplateExcel()
    {
        $field=[
            ['field'=>'parent_id','title'=>'上级'],
            ['field'=>'title','title'=>'名称(必填)'],
            ['field'=>'icon','title'=>'图标'],
            ['field'=>'type','title'=>'类型(必填)'],
            ['field'=>'position','title'=>'按钮位置'],
            ['field'=>'is_drop','title'=>'是否下拉'],
            ['field'=>'btn_class','title'=>'按钮样式'],
            ['field'=>'open_type','title'=>'打开方式'],
            ['field'=>'width','title'=>'弹窗宽'],
            ['field'=>'height','title'=>'弹窗高'],
            ['field'=>'href','title'=>'访问路径'],
            ['field'=>'power_node','title'=>'授权标识'],
            ['field'=>'where','title'=>'显示条件'],
            ['field'=>'sort','title'=>'排序(必填)'],
        ];
        $ExcelService = new ExcelService();
        $ExcelService->Template('菜单管理', $field);
    }

}